﻿package 
{
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Shape;
	
	/**
	 * ...
	 * 
	 */
	public class Escenario extends MovieClip
	{
		var mover:Tween; 
		var mover2:Tween;
		
		private var mosquitos:Array
		
		public function Escenario()
		{
			for (var i:int = 0; i < 20; i++ ) trace("angulo: " + Math.round((Math.random() * 2)))
			addMosquitos()
			moverMosquitos()
			pararMuneco();
			
			stage.addEventListener( MouseEvent.CLICK, clicOnArea);
		}
		
		private function termino (e:TweenEvent){
			pararMuneco();
		}
		
		/*
		 * Este metodo es el encargado de controlar las acciones del movimiento del 
		 * personaje, cuando se hace click sobre cualquier zona del escenario
		 * 
		 */

		private function clicOnArea(e:MouseEvent):void 
		{
			
			select.x = mouseX - select.width/2
			select.y = mouseY - select.height/2
			munecoMc.rotation = (180*Math.atan2(mouseY-munecoMc.y,mouseX-munecoMc.x))/Math.PI + 90;

			if ( mover != null )
				mover.removeEventListener(TweenEvent.MOTION_FINISH, termino);
				
			var dXY:Number = Math.sqrt(Math.pow(mouseX - munecoMc.x, 2) + Math.pow(mouseY - munecoMc.y, 2));

			var time:Number = dXY / 100;
			mover = new Tween(munecoMc, "x", null, munecoMc.x, mouseX,time,true);
			mover2 = new Tween(munecoMc, "y", null, munecoMc.y, mouseY, time, true);
						
			mover.start();
			mover2.start();
			
			munecoMc.play();

			mover.addEventListener(TweenEvent.MOTION_FINISH, termino);
		}
		
		private function addMosquitos() {
			
			mosquitos = new Array()
			
			//Agregamos los mosquitos de la zona 1
			for (var i:int = 0; i < 10; i++) {
				var mosquito:Mosquito = new Mosquito(100,100,350,300, this)

				mosquitos.push(mosquito)
				this.addChild(mosquito)
			}
			/*
			var spr:Shape = new Shape();
			spr.graphics.beginFill(0x00FF00, 3);
			spr.graphics.drawRect(100,100,250,250);
			spr.graphics.endFill();
			this.addChild(spr)
		
			*/
			for (var i:int = 0; i < 5; i++) {
				var mosquito:Mosquito = new Mosquito(530,230,750,350, this)

				mosquitos.push(mosquito)
				this.addChild(mosquito)
			}
		}
		
		private function moverMosquitos() {
			
			for (var i:int; i < mosquitos.length; i++ )
				mosquitos[i].run()
			
		}
		
		private function pararMuneco():void
		{
			munecoMc.gotoAndStop(1);
		}
		
	}
}